package games.gong.durid.controller;

import games.gong.durid.common.R;
import games.gong.durid.common.PermissionConstants;
import games.gong.durid.entity.UserRole;
import games.gong.durid.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户角色管理", description = "用户角色关联管理接口")
@RestController
@RequestMapping("/api/user-roles")
public class UserRoleController {
    
    @Autowired
    private UserRoleService userRoleService;
    
    @Operation(summary = "创建用户角色关联")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_CREATE + "')")
    public R<UserRole> createUserRole(
            @Parameter(description = "用户角色关联信息") @RequestBody UserRole userRole) {
        int result = userRoleService.addUserRole(userRole);
        if (result > 0) {
            return R.success("用户角色关联创建成功", userRole);
        } else {
            return R.error("用户角色关联创建失败");
        }
    }
    
    @Operation(summary = "删除用户角色关联")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_DELETE + "')")
    public R<String> deleteUserRole(
            @Parameter(description = "用户角色关联ID") @PathVariable Long id) {
        int result = userRoleService.deleteUserRole(id);
        if (result > 0) {
            return R.success("用户角色关联删除成功");
        } else {
            return R.error("用户角色关联删除失败");
        }
    }
    
    @Operation(summary = "根据用户ID和角色ID删除用户角色关联")
    @DeleteMapping("/user/{userId}/role/{roleId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_DELETE + "')")
    public R<String> deleteUserRoleByUserIdAndRoleId(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "角色ID") @PathVariable Long roleId) {
        int result = userRoleService.deleteUserRoleByUserIdAndRoleId(userId, roleId);
        if (result > 0) {
            return R.success("用户角色关联删除成功");
        } else {
            return R.error("用户角色关联删除失败");
        }
    }
    
    @Operation(summary = "获取用户角色关联详情")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_READ + "')")
    public R<UserRole> getUserRole(
            @Parameter(description = "用户角色关联ID") @PathVariable Long id) {
        UserRole userRole = userRoleService.getUserRoleById(id);
        if (userRole != null) {
            return R.success(userRole);
        } else {
            return R.error("用户角色关联不存在");
        }
    }
    
    @Operation(summary = "根据用户ID获取用户角色关联列表")
    @GetMapping("/user/{userId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_READ + "')")
    public R<List<UserRole>> getUserRolesByUserId(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        List<UserRole> userRoles = userRoleService.getUserRolesByUserId(userId);
        return R.success(userRoles);
    }
    
    @Operation(summary = "根据角色ID获取用户角色关联列表")
    @GetMapping("/role/{roleId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.USER_ROLE_READ + "')")
    public R<List<UserRole>> getUserRolesByRoleId(
            @Parameter(description = "角色ID") @PathVariable Long roleId) {
        List<UserRole> userRoles = userRoleService.getUserRolesByRoleId(roleId);
        return R.success(userRoles);
    }
}